home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume10 / derez < prev    next >
Encoding:
Internet Message Format  |  1987-06-30  |  13.8 KB

  1. Path: uunet!rs
  2. From: rs@uunet.UU.NET (Rich Salz)
  3. Newsgroups: comp.sources.unix
  4. Subject: v10i030:  Find and remove stale files from a disk.
  5. Message-ID: <522@uunet.UU.NET>
  6. Date: 2 Jul 87 03:04:49 GMT
  7. Organization: UUNET Communications Services, Arlington, VA
  8. Lines: 419
  9. Approved: rs@uunet.uu.net
  10.  
  11. Submitted by: maddog!root
  12. Mod.Sources: Volume 10, Number 30
  13. Archive-Name: derez
  14.  
  15. Derez, a program to find and enmourge stale files on a disk.
  16.  
  17. [  This is great -- my first "anonymous" contribution.  I couldn't find
  18.    "enmourge" in my Webster's Universal; it apparently means remove.
  19.    --  r$ ]
  20.  
  21. #! /bin/sh
  22. # This is a shell archive.  Remove anything before this line, then unpack
  23. # it by saving it into a file and typing "sh file".  To overwrite existing
  24. # files, type "sh file -c".  You can also feed this as standard input via
  25. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  26. # will see the following message at the end:
  27. #        "End of shell archive."
  28. # Contents:  README makefile derez.1 readbyte.1 derez.c readbyte.c
  29. # Wrapped by rs@uunet on Wed Jul  1 22:52:21 1987
  30. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  31. if test -f README -a "${1}" != "-c" ; then 
  32.   echo shar: Will not over-write existing file \"README\"
  33. else
  34. echo shar: Extracting \"README\" \(1140 characters\)
  35. sed "s/^X//" >README <<'END_OF_README'
  36. XDerez, a program to find and enmourge stale files on a disk.
  37. X
  38. XThis code is explicitly placed in the public domain by the author
  39. Xand may be used for fun or profit.  It is explicitly not copyrighted
  40. Xand contains no references to the author's true name.  This is by
  41. Xdesign as the author does not what to become "known" as the author of
  42. XDerez.
  43. X
  44. XAnyone having problems with derez should send a mail request to maddog!root
  45. Xfor possible help or suggestions.  Anyone who fixes some of derez's bugs or
  46. Xinadequacies is invited to send maddog!root a copy of the patches.  We can
  47. Xalways use a better, but not necessarily less Draconian, derez on our system.
  48. X
  49. XDerez does not contain a mechanism to warn users about the impending
  50. Xremoval of stale files.  We have found that doing this only invites the
  51. Xusers to go and defeat the effects of derez totally without archiving
  52. Xtheir stale files to tape and removing them from disk.  On systems with
  53. Xcooperative users, perhaps a separate program that sends users mail about
  54. Xtheir stale files, listing their path names might be helpful.
  55. X
  56. XXXXXXXXXXXXXXXXXXXXX Cut here and feed it to /bin/sh XXXXXXXXXXXXXXXXXXX
  57. END_OF_README
  58. if test 1140 -ne `wc -c <README`; then
  59.     echo shar: \"README\" unpacked with wrong size!
  60. fi
  61. # end of overwriting check
  62. fi
  63. if test -f makefile -a "${1}" != "-c" ; then 
  64.   echo shar: Will not over-write existing file \"makefile\"
  65. else
  66. echo shar: Extracting \"makefile\" \(414 characters\)
  67. sed "s/^X//" >makefile <<'END_OF_makefile'
  68. XCFLAGS = -O
  69. XPROGS = derez readbyte
  70. XMANS = derez.1 readbyte.1
  71. X
  72. Xall : $(PROGS)
  73. X
  74. Xinstall : all $(MANS)
  75. X    install -c derez /usr/local/bin
  76. X    install -c -m 644 derez.1 /usr/man/manl/derez.l
  77. X    install -c readbyte /usr/local/bin
  78. X    install -c -m 644 readbyte.1 /usr/man/manl/readbyte.l
  79. X
  80. Xclean :
  81. X    rm -f *.o
  82. X
  83. Xclobber : clean
  84. X    rm -f $(PROGS)
  85. X
  86. Xderez : derez.c
  87. X    cc -o derez derez.c
  88. X
  89. Xreadbyte : readbyte.c
  90. X    cc -o readbyte readbyte.c
  91. END_OF_makefile
  92. if test 414 -ne `wc -c <makefile`; then
  93.     echo shar: \"makefile\" unpacked with wrong size!
  94. fi
  95. # end of overwriting check
  96. fi
  97. if test -f derez.1 -a "${1}" != "-c" ; then 
  98.   echo shar: Will not over-write existing file \"derez.1\"
  99. else
  100. echo shar: Extracting \"derez.1\" \(3578 characters\)
  101. sed "s/^X//" >derez.1 <<'END_OF_derez.1'
  102. X.TH DEREZ LOCAL "2 February 1987"
  103. X.UC 4
  104. X.SH NAME
  105. Xderez \- find stale files, and move them into a MORGUE directory
  106. X.SH SYNOPSIS
  107. X.br
  108. X\fBderez [-t days] [dir1 dir2 dir3 ...]\fP
  109. X.SH DESCRIPTION
  110. X.I Derez
  111. Xis a program which is used to archive stale files.
  112. XMost if not \fIall\fP UNIX systems suffer from the finite disk size problem.
  113. XIf this problem is aggravated by users which have stale files that have not
  114. Xbeen read for some time,
  115. Xderez offers a solution.
  116. XIt moves the stale files to a \fIMORGUE\fP directory so that they can
  117. Xlater be archived using \fItar\fP.
  118. X.sp
  119. X\fIDerez\fP uses the access times,
  120. Xtime last \fIread\fP not time last \fBwritten\fP,
  121. Xof the files to determine their staleness.
  122. XThe default number of days since last access is 180 days,
  123. Xbut this can be changed using the \fB-t\fP option.
  124. XIf no directory list is given to derez it searches current directory,
  125. Xexcept the subdirectory \fIMORGUE\fP.
  126. XThe \fB-t\fP option and a directory list can be used to give special handling to
  127. Xantisocial users.
  128. X.sp
  129. XDerez can be defeated by using \fIfind\fP and \fIreadbyte\fP to find and read all
  130. Xof the files in a directory tree.
  131. XAlthough doing this might be reasonable for some directory that you haven't
  132. Xlooked at in some time, but will be using in a few days,
  133. Xdoing this to \fBall\fP of your files to defeat derez is considered antisocial
  134. Xand is dealt with using back up options.
  135. XThese options include:
  136. Xchanging derez's definition of staleness from several months to one day or less for the
  137. Xantisocial user,
  138. Xarchiving directories of the antisocial user by hand and
  139. Xremoval of the antisocial user's account from the machine.
  140. XIf you are going to use \fIreadbyte\fP to save some files from derez,
  141. Xyou should check with \fBroot\fP first to make sure that the action won't be
  142. Xinterpreted as antisocial.
  143. X.sp
  144. XThe best way to avoid getting derez'ed is to actively archive your stale files
  145. Xonto tape using tar and keep them off line.
  146. XIf a user actively archives his stale files onto tape,
  147. Xa rather large disk usage for truly active files will be tolerated.
  148. X.sp
  149. XAlthough derez was written for \fBroot's\fP use,
  150. Xan individual user can use it to find and archive his stale files.
  151. XThis is done by making a subdirectory with the name \fIMORGUE\fP in any directory
  152. Xyou would like to archive the descendant stale files.
  153. XYou then run \fIderez\fP.
  154. XDerez will create a list of stale files,
  155. Xwith the name \fIMORGUE/DEREZ\fP and then move the stale files into the directory
  156. XMORGUE.
  157. XIf a file that is removed is the last file in it's parent directory,
  158. Xthe parent directory is removed.
  159. XThe contents of the morgue directory can be archived using tar and then removed from the disk.
  160. XThe use of \fIderez\fP for stale file control is highly recommended.
  161. XScratch tapes can be found next to the system console.
  162. X.sp
  163. XFiles that have been derez'ed by root can be recovered from the archive tape using
  164. X\fItar\fP.
  165. XTo recover all one's stale files without moving some or all off line
  166. Xwith personal archive tapes is considered antisocial.
  167. X.SH SEE ALSO
  168. Xfind, readbyte
  169. X.ta 3.0i
  170. X.SH FILES
  171. X/usr/local/morgue/derez.mm.dd.yy    Lists of files derez'ed by root.
  172. X.br
  173. XMORGUE/DEREZ    The files derez will enmorgue.
  174. X.br
  175. XMORGUE/*    The enmorgued files.
  176. X.SH "BUGS(Not to be taken advantage of)"
  177. XDEREZ shares tar's limits on path name lengths.
  178. X.br
  179. XDerez does not archive files with more than one hard link as they
  180. Xwould get seperated into several copies of the files in the current
  181. Ximplementation.
  182. X.br
  183. XMORGUE and DEREZ are reserved names.
  184. X.br
  185. XDerez will not move files with path names of the form MORGUE/* or DEREZ
  186. Xfor obvious reasons.
  187. END_OF_derez.1
  188. if test 3578 -ne `wc -c <derez.1`; then
  189.     echo shar: \"derez.1\" unpacked with wrong size!
  190. fi
  191. # end of overwriting check
  192. fi
  193. if test -f readbyte.1 -a "${1}" != "-c" ; then 
  194.   echo shar: Will not over-write existing file \"readbyte.1\"
  195. else
  196. echo shar: Extracting \"readbyte.1\" \(631 characters\)
  197. sed "s/^X//" >readbyte.1 <<'END_OF_readbyte.1'
  198. X.TH READBYTE LOCAL "2 February 1987"
  199. X.UC 4
  200. X.SH NAME
  201. Xreadbyte \- Read the first byte of a file, removing its staleness
  202. X.SH SYNOPSIS
  203. X.br
  204. X\fBreadbyte filename\fP
  205. X.SH DESCRIPTION
  206. X.I Readbyte
  207. Xreads the first byte of the specified file,
  208. Xchanging its access date.
  209. XIt is used to remove staleness of a file to prevent derez from
  210. Xputting the file in the morgue for archival onto tape.
  211. XExcessive use of \fIreadbyte\fP, or any other means of updating
  212. Xthe access date of a file to defeat \fIderez\fP,
  213. Xis interpreted as antisocial by root.
  214. XAntisocial behavior is dealt with as described in the man page for \fIderez\fP.
  215. X.SH SEE ALSO
  216. Xfind, derez
  217. END_OF_readbyte.1
  218. if test 631 -ne `wc -c <readbyte.1`; then
  219.     echo shar: \"readbyte.1\" unpacked with wrong size!
  220. fi
  221. # end of overwriting check
  222. fi
  223. if test -f derez.c -a "${1}" != "-c" ; then 
  224.   echo shar: Will not over-write existing file \"derez.c\"
  225. else
  226. echo shar: Extracting \"derez.c\" \(4388 characters\)
  227. sed "s/^X//" >derez.c <<'END_OF_derez.c'
  228. X/* This program archives and removes all files on /u0
  229. Xthat have not been accessed in 180 days.
  230. X    */
  231. X
  232. X#include <sys/types.h>
  233. X#include <sys/stat.h>
  234. X#include <sys/file.h>
  235. X#include <strings.h>
  236. X
  237. X#define MAXLEN 10240
  238. X#define USAGE "derez [-t days] [dir1 dir2 dir3 ...]\n"
  239. X
  240. X#include <stdio.h>
  241. X
  242. Xmain(argc, argv)
  243. Xint argc;
  244. Xchar **argv;
  245. X{
  246. X    FILE *fptr;
  247. X    struct stat statbuf;
  248. X    char filename[MAXLEN];
  249. X    char cmd[MAXLEN];
  250. X    char dirlist[MAXLEN];
  251. X    int since;
  252. X
  253. X    dirlist[0] = NULL;
  254. X    since = 180;
  255. X
  256. X
  257. X    /* Check for the existence and access permissions on the MORGUE.
  258. X        */
  259. X    if(stat("MORGUE", &statbuf)) {
  260. X        perror("derez: MORGUE");
  261. X        exit(1);
  262. X    }
  263. X    if(!(statbuf.st_mode & S_IFDIR)) {
  264. X        fprintf(stderr, "derez: MORGUE is not a directory\n");
  265. X        exit(1);
  266. X    }
  267. X    if(access("MORGUE", R_OK | W_OK | X_OK)) {
  268. X        fprintf(stderr, "derez: The MORGUE must have read, write and execute perms\n");
  269. X        exit(1);
  270. X    }
  271. X
  272. X    if(argc > 1) {
  273. X        if(strcmp(argv[1], "-t") == 0) {
  274. X            if(argc < 3) {
  275. X                fprintf(stderr, USAGE);
  276. X                exit(1);
  277. X            }
  278. X            if(sscanf(argv[2], "%d", &since) != 1) {
  279. X                fprintf(stderr, "derez: bad arg for -t option %s\n", argv[2]);
  280. X                exit(1);
  281. X            }
  282. X            if(since < 1 || since > 1000) {
  283. X                fprintf(stderr, "derez: bad value for -t argument\n");
  284. X                exit(1);
  285. X            }
  286. X            argv += 2;
  287. X            argc -= 2;
  288. X        }
  289. X        while(argc > 1) {
  290. X            if((strncmp(argv[1], "/", strlen("/")) == 0) || (strncmp(argv[1], "../", strlen("../")) == 0)) {
  291. X                fprintf(stderr, "derez: Directories must be relative to .\n");
  292. X                exit(1);
  293. X            }
  294. X            if(lstat(argv[1], &statbuf)) {
  295. X                fprintf(stderr, "The directory %s must exist\n", argv[1]);
  296. X                exit(1);
  297. X            }
  298. X            if(statbuf.st_mode & S_IFDIR) {
  299. X                if(dirlist[0] != NULL)
  300. X                    strcat(dirlist, " ");
  301. X                strcat(dirlist, argv[1]);
  302. X                argv += 1;
  303. X                argc -= 1;
  304. X            }
  305. X            else {
  306. X                fprintf(stderr, "derez: %s is not a directory\n", argv[1]);
  307. X                exit(1);
  308. X            }
  309. X        }
  310. X    }
  311. X    /* We currently only derez files with 1 link as derez would
  312. X    break up two links to the same file into seperate files.
  313. X    We will have to steal code from tar to do an internal link table
  314. X    and drop the use of tar completely.
  315. X        */
  316. X    sprintf(cmd, "find %s -type f -links 1 -atime +%d -print > MORGUE/DEREZ",
  317. X        (dirlist[0] == NULL ? "." : dirlist), since);
  318. X
  319. X    printf("%s\n", cmd);
  320. X    if(system(cmd) != 0) {
  321. X        fprintf(stderr, "derez: The attempt to create MORGUE/DEREZ failed\n");
  322. X        exit(1);
  323. X    }
  324. X    if((fptr = fopen("MORGUE/DEREZ", "r")) == NULL) {
  325. X        fprintf(stderr, "Open of MORGUE/DEREZ for reading failed\n");
  326. X        exit(1);
  327. X    }
  328. X    while(fgets(filename, MAXLEN, fptr) != NULL) {
  329. X        char *rightslash;
  330. X        /* Kill off the newline. */
  331. X        if(filename[strlen(filename) - 1] != '\n') {
  332. X            fprintf(stderr, "missing \\n on %s\n", filename);
  333. X            continue;
  334. X        }
  335. X        filename[strlen(filename) - 1] = NULL;
  336. X
  337. X        /* If the file path name is too long we don't want
  338. X        to see the error message from tar.
  339. X            */
  340. X        if(strlen(filename) > 99)
  341. X            continue;
  342. X
  343. X        /* If the file basename is of the form .* we want to
  344. X        leave it alone to prevent removal of someones login
  345. X        directory or setup files.  Of course if you take this
  346. X        code out you get automatic removal of directories
  347. X        associated with inactive accounts.
  348. X            */
  349. X        if((rightslash = rindex(filename, '/')) != NULL) {
  350. X            if(rightslash[1] == '.')
  351. X                continue;
  352. X        }
  353. X        
  354. X        if(
  355. X            /* We don't want to mess with files already in the MORGUE.
  356. X                */
  357. X            (strncmp(filename, "./MORGUE/", strlen("./MORGUE/")) != 0)
  358. X            && (strncmp(filename, "MORGUE/", strlen("MORGUE/")) != 0)
  359. X            /* The lost+found directory is a sacred system item.
  360. X                */
  361. X            && (strncmp(filename, "./lost+found/", strlen("./lost+found/")) != 0)
  362. X            && (strncmp(filename, "lost+found/", strlen("lost+found/")) != 0)
  363. X            /* If we were to enmorgue a file named DEREZ, we
  364. X            would overwrite the list of files to move.
  365. X                */
  366. X            && (strncmp(filename, "./DEREZ") != 0)
  367. X            && (strcmp(filename, "DEREZ") != 0)
  368. X        ){
  369. X            sprintf(cmd, "tar cf - %s | (cd MORGUE; tar xpf -)", filename);
  370. X            if(system(cmd) == 0) {
  371. X                fprintf(stderr, "enmorgue of %s succeeded\n", filename);
  372. X                unlink(filename);
  373. X                /* If it was the last file in the directory
  374. X                we also want to remove the directory.
  375. X                    */
  376. X                while(strlen(filename) > 0 && filename[strlen(filename) - 1] != '/') {
  377. X                    filename[strlen(filename) - 1] = NULL;
  378. X                }
  379. X                filename[strlen(filename) - 1] = NULL;
  380. X                rmdir(filename);
  381. X            }
  382. X            else {
  383. X                fprintf(stderr, "enmorgue of %s FAILED, file removal was inhibited\n", filename);
  384. X            }
  385. X        }
  386. X    }
  387. X    fclose(fptr);
  388. X}
  389. END_OF_derez.c
  390. if test 4388 -ne `wc -c <derez.c`; then
  391.     echo shar: \"derez.c\" unpacked with wrong size!
  392. fi
  393. # end of overwriting check
  394. fi
  395. if test -f readbyte.c -a "${1}" != "-c" ; then 
  396.   echo shar: Will not over-write existing file \"readbyte.c\"
  397. else
  398. echo shar: Extracting \"readbyte.c\" \(363 characters\)
  399. sed "s/^X//" >readbyte.c <<'END_OF_readbyte.c'
  400. X#include <stdio.h>
  401. X
  402. Xmain(argc,argv)
  403. Xint argc;
  404. Xchar *argv[];
  405. X{
  406. X    int fd;
  407. X    char cbuffer[512];
  408. X
  409. X    if(argc != 2) {
  410. X        fprintf(stderr,"usage: readbyte file\n");
  411. X        exit(1);
  412. X    }
  413. X    if((fd=open(argv[1],0)) < 0) {
  414. X        fprintf(stderr,"cant open %s\n",argv[1]);
  415. X        exit(1);
  416. X    }
  417. X    if(read(fd,cbuffer,1) != 1) {
  418. X        fprintf(stderr,"cant read byte from file %s\n",argv[1]);
  419. X        exit(1);
  420. X    }
  421. X}
  422. END_OF_readbyte.c
  423. if test 363 -ne `wc -c <readbyte.c`; then
  424.     echo shar: \"readbyte.c\" unpacked with wrong size!
  425. fi
  426. # end of overwriting check
  427. fi
  428. echo shar: End of shell archive.
  429. exit 0
  430.